#Include 'Protheus.ch'

/*
+----------------------------------------------------------------------------+
!                             FICHA TECNICA DO PROGRAMA                      !
+----------------------------------------------------------------------------+
!Programa          ! FT050TOK                                                !
+------------------+---------------------------------------------------------+
!Descricao         ! PONTO DE ENTRADA PARA INSERIR PREVISAO DE VENDA			!
+------------------+---------------------------------------------------------+
!Autor             ! TSC705 - Cesar Augusto Silva 					          !
+------------------+---------------------------------------------------------+
!Data de Criacao   ! 11/06/2013                                              !
+------------------+---------------------------------------------------------+
!   ATUALIZACOES                                                             !
+-------------------------------------------+-----------+-----------+--------+
!   Descricao detalhada da atualizacao      !Nome do    ! Analista  !Data da !
!                                           !Solicitante! Respons.  !Atualiz.!
+-------------------------------------------+-----------+-----------+--------+
!                                           !           !           !        !
!                                           !           !           !        !
+-------------------------------------------+-----------+-----------+--------+
!                                           !           !           !        !
!                                           !           !           !        !
+-------------------------------------------+-----------+-----------+--------+
*/

User Function FT050TOK()

	Local bAcao := {|| prevVenda() }
	Local cTitulo := 'Cadastrando Previsao de Venda'
	Local cMsg := 'Processando'
	Local lAborta := .T.

	Processa( bAcao, cTitulo, cMsg, lAborta )

return .T.

Static Function prevVenda()

	Local oMdl		:= FWModelActive()
	Local oMdlCab	:= oMdl:GetModel('SCTCAB')
	Local oMdlGrid	:= oMdl:GetModel('SCTGRID')

	Local nX

	dbSelectArea("SCT")
	dbSetOrder(1)

	For nX := 1 to oMdlGrid:GetQtdLine()

		oMdlGrid:GoLine(nX)

		SC4->(dbOrderNickName('SCT'))

		If SC4->(dbSeek(xFilial("SC4") + oMdlCab:GetValue("CT_DOC") + oMdlGrid:GetValue("CT_SEQUEN")))

			If oMdlGrid:aCols[nX][17]

				RecLock( 'SC4' , .F. )
					SC4->( dbDelete() )
				msUnlock()

			Else

				RecLock("SC4" , .F.)			// Altera previsao de venda
					SC4->C4_PRODUTO	:=  oMdlGrid:GetValue("CT_PRODUTO")
					SC4->C4_QUANT 	:=  oMdlGrid:GetValue("CT_QUANT")
					SC4->C4_VALOR  	:=  oMdlGrid:GetValue("CT_VALOR")
					SC4->C4_DATA	:=  oMdlGrid:GetValue("CT_DATA")
					SC4->C4_SEQUEN	:=  oMdlGrid:GetValue("CT_SEQUEN")
					SC4->C4_DOC		:=  oMdlCab:GetValue("CT_DOC")
				IncProc()
				msUnlock()

				SCT->CT_ALTERAD	:=  "S"

			Endif

		Else

			RecLock("SC4" , .T.)			// Insere previsao de venda
				SC4->C4_FILIAL	:=  xFilial("SC4")
				SC4->C4_PRODUTO	:=  oMdlGrid:GetValue("CT_PRODUTO")
				SC4->C4_QUANT 	:=  oMdlGrid:GetValue("CT_QUANT")
				SC4->C4_VALOR  	:=  oMdlGrid:GetValue("CT_VALOR")
				SC4->C4_DATA	:=  oMdlGrid:GetValue("CT_DATA")
				SC4->C4_SEQUEN	:=  oMdlGrid:GetValue("CT_SEQUEN")
				SC4->C4_DOC		:=  oMdlCab:GetValue("CT_DOC")
			INCPROC()
			MSUnlock()

		Endif

	Next nX

Return
